 // pages/card/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    back: 0,
    cardInfo:{},
    p_user_id:0,
    popup:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.data.cardId = options.cardId ? options.cardId : 3;
    this.data.p_user_id = options.p_user_id ? options.p_user_id : 0;
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      var arr = scene.split(",");
      this.data.cardId = arr[0];
      this.data.p_user_id = arr[1] ? arr[1]:0;
    }

    this.setData({ shopInfo: app.getShopInfo()})
    app.getUserInfo(function (user_info) {
      that.loadCardInfo();
      that.data.user_info = user_info;
    })
  
    if (options.back) {
      this.data.back = 1;
    }
  },
  loadCardInfo: function (reload) {
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        cardInfo: {},
      });
    }
    if (this.data.loading == 1) {
      return;
    }
    this.data.loading = 1;
    app.api("mall/card/find", {
      cardId: this.data.cardId
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }

      that.setData({
        cardInfo: res.data,
      });
    })

  },

  addCard: function (e) {
    var card_id = e.currentTarget.dataset.card_id;
    //领取卡券
    app.api("mall/card/addCard", {
      cardId: card_id,
      p_user_id: this.data.p_user_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      app.alert("领取成功");
      my.navigateTo({
        url: '/pages/card/index',
      })
    })  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.cardInfo(1);
    my.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    this.btn_cancel();
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var title = this.data.user_info.nickname + "分享了一张[" + that.data.cardInfo.name + "]优惠券,快来领取吧";
    return {
      title: title,
      path: '/pages/card/info?cardId=' + that.data.cardId + '&p_user_id=' + this.data.user_info.user_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  btn_cancel:function(){
    this.setData({ popup:0});
  },
  showShare:function(){
    this.setData({ popup: 1 });
  },
  makePic:function(){
     my.navigateTo({
       url: '/pages/draw/index?page=' + encodeURIComponent('pages/card/info') + '&scene=' + this.data.cardId + '_' + this.data.user_info.user_id + "&title=" + encodeURIComponent(this.data.cardInfo.name) + "&picname=" + encodeURIComponent(this.data.cardInfo.pic),
     })
  }
})